package org.example.word;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.example.word.model.CaseNotice;
import org.joda.time.DateTime;

import java.io.*;
import java.net.URL;
import java.nio.file.Files;
import java.util.List;

public class WriteWordDemo {
    public static final String CONTENT = "{\"address\":\"山东省临沭县文化旅游发展中心\",\"age\":\"52\",\"attachFile\":\"无附件\",\"category\":\"困难求助\",\"content\":\"您好！因组织部门查档，急需1989年普通高等学校招生工作规定（报名条件），网上搜了三天未果，恳请您帮助。谢谢！\",\"email\":\"\",\"idNumber\":\"\",\"idType\":\"身份证\",\"ip\":\"--------\",\"isPublic\":\"是\",\"mobile\":\"13969912088\",\"name\":\"王健\",\"postCode\":\"276700\",\"replyAttachFile\":\"无附件\",\"replyContent\":\"请及时办理\",\"replyContent1\":\"2004年以前我院文书档案已移交省档案局，建议考生持身份证到省档案局查询。\",\"replyTime\":\"2021-04-1909:06:49\",\"replyUnit\":\"办公室（机关服务中心）\",\"serial\":\"JYT20210420127\",\"sex\":\"男\",\"submitTime\":\"2021-04-1717:32:34\",\"target\":\"默认\",\"telephone\":\"\",\"title\":\"普通高考报名条件咨询\"}";
    //public static final String FILE_PATH = "https://edusafety-school.oss-cn-beijing.aliyuncs.com/template/dealt_notice_tpl.doc";
    public static final String FILE_PATH = "C:\\Users\\JohnConnor\\Downloads\\dealt_notice_tpl.doc";
    public static void main(String[] args) {
        /*try (BufferedInputStream inputStream = new BufferedInputStream(new URL(FILE_PATH).openStream())) {*/
        try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(new File(FILE_PATH)))) {
            try(HWPFDocument document = new HWPFDocument(inputStream)){
                Range range = document.getRange();
                TableIterator iterator = new TableIterator(range);
                while (iterator.hasNext()) {
                    Table table = iterator.next();
                    if (table != null) {
                        CaseNotice caseNotice = new CaseNotice();
                        caseNotice.setTitle("普通高考报名条件咨询");
                        caseNotice.setContent("您好！因组织部门查档，急需1989年普通高等学校招生工作规定（报名条件），网上搜了三天未果，恳请您帮助。谢谢！");
                        caseNotice.setName("杨金勇");
                        caseNotice.setTelephone("0531-84421536");
                        caseNotice.setMobile("15168890236");
                        caseNotice.setAddress("山东省济南市历下区舜耕路111号");
                        caseNotice.setEmail("JohnConnor@126.com");
                        caseNotice.setPostCode("0010");
                        caseNotice.setIdType("身份证");
                        caseNotice.setIdNumber("370125198202236613");
                        caseNotice.setSex("男");
                        caseNotice.setAge("40");
                        caseNotice.setAttachFile("无附件");
                        caseNotice.setCategory("困难救助");
                        caseNotice.setTarget("中小学生");
                        caseNotice.setIsPublic("是");
                        caseNotice.setSubmitTime(DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
                        caseNotice.setSerial("JYT" + DateTime.now().toString("yyyyMMddHHmmss"));
                        caseNotice.setIp("192.168.51.47");
                        /*table.getRow(1).getCell(1).replaceText(caseNotice.getTitle(),true);
                        table.getRow(2).getCell(1).replaceText(caseNotice.getContent(),true);
                        table.getRow(3).getCell(1).replaceText(caseNotice.getName(),true);
                        table.getRow(4).getCell(1).replaceText(caseNotice.getTelephone(),true);
                        table.getRow(5).getCell(1).replaceText(caseNotice.getMobile(),true);
                        table.getRow(6).getCell(1).replaceText(caseNotice.getAddress(),true);
                        table.getRow(7).getCell(1).replaceText(caseNotice.getEmail(),true);
                        table.getRow(8).getCell(1).replaceText(caseNotice.getPostCode(),true);
                        table.getRow(9).getCell(1).replaceText(caseNotice.getIdType(),true);
                        table.getRow(10).getCell(1).replaceText(caseNotice.getIdNumber(),true);
                        table.getRow(11).getCell(1).replaceText(caseNotice.getSex(),true);
                        table.getRow(12).getCell(1).replaceText(caseNotice.getAge(),true);
                        table.getRow(13).getCell(1).replaceText(caseNotice.getAttachFile(),true);
                        table.getRow(14).getCell(1).replaceText(caseNotice.getCategory(),true);*/
                        table.getRow(15).getCell(1).replaceText("${target}",caseNotice.getTarget());
                        table.getRow(15).getCell(3).replaceText("${isPublic}",caseNotice.getIsPublic());
                        table.getRow(16).getCell(1).replaceText("${submitTime}",caseNotice.getSubmitTime());
                        table.getRow(16).getCell(3).replaceText("${workNum}",caseNotice.getSerial());
                        //table.getRow(17).getCell(1).replaceText(caseNotice.getIp(),true);

                    }
                }
                String targetPath = "E:\\work_template\\" + System.currentTimeMillis() + ".doc";
                try (FileOutputStream outputStream = new FileOutputStream(targetPath)) {
                    document.write(outputStream);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
